package com.ouc.dcrms.video;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.ptr.IntByReference;

import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

/*****************************************************************************
 * 类 ：JDialogPlayBackByTime 
 * 类描述 ：远程按时间回放操作
 ****************************************************************************/
public class JDialogPlayBackByTime extends JDialog {
    private static final long serialVersionUID = 8602009267485067229L;

    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    NativeLong m_lUserID;// 用户ID
    NativeLong m_lPlayHandle;// 播放句柄
    NativeLong m_lLoadHandle;// 下载句柄
    String m_sDeviceIP;// 设备IP
    int m_iChanShowNum;// 回放通道
    boolean m_bSound;// 是否开启声音
    boolean m_bPause;// 是否已暂停
    boolean m_bTimeSave;// 是否在保存

    Timer Downloadtimer;// 下载用定时器
    Timer Playbacktimer;// 回放用定时器

    /*************************************************
     * 函数: JDialogPlayBackByTime 函数描述: 构造函数 Creates new form
     * JDialogPlayBackByTime
     *************************************************/
    public JDialogPlayBackByTime(java.awt.Frame parent, boolean modal,
	    NativeLong lUserID, String sIP) {
	super(parent, modal);
	initComponents();
	initialDialog();
	m_lUserID = lUserID;
	m_lPlayHandle = new NativeLong(-1);
	m_lLoadHandle = new NativeLong(-1);
	m_iChanShowNum = 0;
	m_sDeviceIP = sIP;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

	jPanelTime = new JPanel();
	jTextFieldsHour = new JTextField();
	jTextFieldeHour = new JTextField();
	jTextFieldeYear = new JTextField();
	jLabelEndTime = new JLabel();
	jLabel1 = new JLabel();
	jTextFieldsYear = new JTextField();
	jLabelStartTime = new JLabel();
	jLabel3 = new JLabel();
	jLabel6 = new JLabel();
	jTextFieldsSecond = new JTextField();
	jTextFieldsDay = new JTextField();
	jLabel9 = new JLabel();
	jLabel12 = new JLabel();
	jLabel8 = new JLabel();
	jTextFieldeSecond = new JTextField();
	jTextFieldsMonth = new JTextField();
	jTextFieldeDay = new JTextField();
	jTextFieldsMinute = new JTextField();
	jLabel5 = new JLabel();
	jLabel11 = new JLabel();
	jLabel2 = new JLabel();
	jLabel7 = new JLabel();
	jLabel10 = new JLabel();
	jTextFieldeMinute = new JTextField();
	jTextFieldeMonth = new JTextField();
	jLabel4 = new JLabel();
	jLabel13 = new JLabel();
	jTextFieldChanNum = new JTextField();
	jPanelPlayControl = new JPanel();
	panelPlay = new java.awt.Panel();
	jSliderAudio = new JSlider();
	jToolBar1 = new JToolBar();
	jButtonPlay = new JButton();
	jButtonStop = new JButton();
	jLabel14 = new JLabel();
	jButtonSlow = new JButton();
	jButtonFast = new JButton();
	jLabel15 = new JLabel();
	jButtonCapture = new JButton();
	jLabel16 = new JLabel();
	jButtonAudio = new JButton();
	jButtonDownload = new JButton();
	jButtonSave = new JButton();
	jButtonExit = new JButton();

	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	setTitle("按时间回放");

	jPanelTime.setBorder(BorderFactory.createTitledBorder("录像时间"));

	jLabelEndTime.setText("结束时间");

	jLabel1.setText("年");

	jLabelStartTime.setText("开始时间");

	jLabel3.setText("日");

	jLabel6.setText("秒");

	jLabel9.setText("日");

	jLabel12.setText("秒");

	jLabel8.setText("月");

	jLabel5.setText("分");

	jLabel11.setText("分");

	jLabel2.setText("月");

	jLabel7.setText("年");

	jLabel10.setText("时");

	jLabel4.setText("时");

	jLabel13.setText("通道号");

	jTextFieldChanNum.setText("1");

	GroupLayout jPanelTimeLayout = new GroupLayout(jPanelTime);
	jPanelTime.setLayout(jPanelTimeLayout);
	jPanelTimeLayout
		.setHorizontalGroup(jPanelTimeLayout
			.createParallelGroup(GroupLayout.Alignment.LEADING)
			.addGroup(
				jPanelTimeLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
						jPanelTimeLayout
							.createParallelGroup(
								GroupLayout.Alignment.LEADING)
							.addGroup(
								jPanelTimeLayout
									.createSequentialGroup()
									.addComponent(
										jLabelStartTime,
										GroupLayout.PREFERRED_SIZE,
										60,
										GroupLayout.PREFERRED_SIZE)
									.addComponent(
										jTextFieldsYear,
										GroupLayout.PREFERRED_SIZE,
										40,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel1,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldsMonth,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel2,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addComponent(
										jTextFieldsDay,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addGroup(
										jPanelTimeLayout
											.createParallelGroup(
												GroupLayout.Alignment.LEADING)
											.addGroup(
												jPanelTimeLayout
													.createSequentialGroup()
													.addGap(20,
														20,
														20)
													.addComponent(
														jTextFieldsHour,
														GroupLayout.PREFERRED_SIZE,
														30,
														GroupLayout.PREFERRED_SIZE))
											.addComponent(
												jLabel3,
												GroupLayout.PREFERRED_SIZE,
												30,
												GroupLayout.PREFERRED_SIZE))
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel4,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldsMinute,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel5,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldsSecond,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel6,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(
										LayoutStyle.ComponentPlacement.UNRELATED)
									.addComponent(
										jLabel13)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldChanNum,
										GroupLayout.PREFERRED_SIZE,
										54,
										GroupLayout.PREFERRED_SIZE))
							.addGroup(
								jPanelTimeLayout
									.createSequentialGroup()
									.addComponent(
										jLabelEndTime,
										GroupLayout.PREFERRED_SIZE,
										60,
										GroupLayout.PREFERRED_SIZE)
									.addComponent(
										jTextFieldeYear,
										GroupLayout.PREFERRED_SIZE,
										40,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel7,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldeMonth,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel8,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addComponent(
										jTextFieldeDay,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addGroup(
										jPanelTimeLayout
											.createParallelGroup(
												GroupLayout.Alignment.LEADING)
											.addComponent(
												jLabel9,
												GroupLayout.PREFERRED_SIZE,
												30,
												GroupLayout.PREFERRED_SIZE)
											.addGroup(
												jPanelTimeLayout
													.createSequentialGroup()
													.addGap(20,
														20,
														20)
													.addComponent(
														jTextFieldeHour,
														GroupLayout.PREFERRED_SIZE,
														30,
														GroupLayout.PREFERRED_SIZE)))
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel10,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldeMinute,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel11,
										GroupLayout.PREFERRED_SIZE,
										20,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jTextFieldeSecond,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)
									.addGap(10,
										10,
										10)
									.addComponent(
										jLabel12,
										GroupLayout.PREFERRED_SIZE,
										30,
										GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(23, Short.MAX_VALUE)));
	jPanelTimeLayout
		.setVerticalGroup(jPanelTimeLayout
			.createParallelGroup(GroupLayout.Alignment.LEADING)
			.addGroup(
				jPanelTimeLayout
					.createSequentialGroup()
					.addGroup(
						jPanelTimeLayout
							.createParallelGroup(
								GroupLayout.Alignment.LEADING)
							.addComponent(
								jLabelStartTime,
								GroupLayout.PREFERRED_SIZE,
								20,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(
								jTextFieldsYear,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel1)
							.addComponent(
								jTextFieldsMonth,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel2)
							.addComponent(
								jTextFieldsDay,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(
								jTextFieldsHour,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel3)
							.addComponent(jLabel4)
							.addComponent(
								jTextFieldsMinute,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel5)
							.addComponent(
								jTextFieldsSecond,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addGroup(
								jPanelTimeLayout
									.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
									.addComponent(
										jLabel6)
									.addComponent(
										jTextFieldChanNum,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
									.addComponent(
										jLabel13)))
					.addPreferredGap(
						LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
						jPanelTimeLayout
							.createParallelGroup(
								GroupLayout.Alignment.LEADING)
							.addComponent(
								jLabelEndTime,
								GroupLayout.PREFERRED_SIZE,
								20,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(
								jTextFieldeYear,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel7)
							.addComponent(
								jTextFieldeMonth,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel8)
							.addComponent(
								jTextFieldeDay,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel9)
							.addComponent(
								jTextFieldeHour,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel10)
							.addComponent(
								jTextFieldeMinute,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel11)
							.addComponent(
								jTextFieldeSecond,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel12))));

	jPanelPlayControl.setBorder(BorderFactory.createTitledBorder(null, "",
		TitledBorder.DEFAULT_JUSTIFICATION,
		TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 0, 12),
		new java.awt.Color(204, 255, 204))); // NOI18N

	panelPlay.setBackground(new java.awt.Color(234, 255, 255));

	GroupLayout panelPlayLayout = new GroupLayout(panelPlay);
	panelPlay.setLayout(panelPlayLayout);
	panelPlayLayout.setHorizontalGroup(panelPlayLayout.createParallelGroup(
		GroupLayout.Alignment.LEADING).addGap(0, 520, Short.MAX_VALUE));
	panelPlayLayout.setVerticalGroup(panelPlayLayout.createParallelGroup(
		GroupLayout.Alignment.LEADING).addGap(0, 378, Short.MAX_VALUE));

	jSliderAudio.addMouseListener(new java.awt.event.MouseAdapter() {
	    public void mouseReleased(java.awt.event.MouseEvent evt) {
		jSliderAudioMouseReleased(evt);
	    }
	});

	jToolBar1.setRollover(true);

	jButtonPlay.setBackground(new java.awt.Color(204, 204, 255));
	jButtonPlay.setFont(new java.awt.Font("微软雅黑", 0, 12));
	jButtonPlay.setForeground(new java.awt.Color(51, 51, 255));
	jButtonPlay.setText("Play");
	jButtonPlay.setFocusable(false);
	jButtonPlay.setHorizontalTextPosition(SwingConstants.CENTER);
	jButtonPlay.setVerticalTextPosition(SwingConstants.BOTTOM);
	jButtonPlay.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonPlayActionPerformed(evt);
	    }
	});
	jToolBar1.add(jButtonPlay);

	jButtonStop.setFont(new java.awt.Font("微软雅黑", 0, 12));
	jButtonStop.setForeground(new java.awt.Color(51, 51, 255));
	jButtonStop.setText("Stop");
	jButtonStop.setFocusable(false);
	jButtonStop.setHorizontalTextPosition(SwingConstants.CENTER);
	jButtonStop.setVerticalTextPosition(SwingConstants.BOTTOM);
	jButtonStop.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonStopActionPerformed(evt);
	    }
	});
	jToolBar1.add(jButtonStop);

	jLabel14.setText("      ");
	jToolBar1.add(jLabel14);

	jButtonSlow.setFont(new java.awt.Font("微软雅黑", 0, 12));
	jButtonSlow.setForeground(new java.awt.Color(51, 51, 255));
	jButtonSlow.setText("Slow");
	jButtonSlow.setFocusable(false);
	jButtonSlow.setHorizontalTextPosition(SwingConstants.CENTER);
	jButtonSlow.setVerticalTextPosition(SwingConstants.BOTTOM);
	jButtonSlow.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonSlowActionPerformed(evt);
	    }
	});
	jToolBar1.add(jButtonSlow);

	jButtonFast.setFont(new java.awt.Font("微软雅黑", 0, 12));
	jButtonFast.setForeground(new java.awt.Color(51, 51, 255));
	jButtonFast.setText("Fast");
	jButtonFast.setFocusable(false);
	jButtonFast.setHorizontalTextPosition(SwingConstants.CENTER);
	jButtonFast.setVerticalTextPosition(SwingConstants.BOTTOM);
	jButtonFast.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonFastActionPerformed(evt);
	    }
	});
	jToolBar1.add(jButtonFast);

	jLabel15.setText("         ");
	jToolBar1.add(jLabel15);

	jButtonCapture.setFont(new java.awt.Font("微软雅黑", 0, 12));
	jButtonCapture.setForeground(new java.awt.Color(51, 51, 255));
	jButtonCapture.setText("Capture");
	jButtonCapture.setFocusable(false);
	jButtonCapture.setHorizontalTextPosition(SwingConstants.CENTER);
	jButtonCapture.setVerticalTextPosition(SwingConstants.BOTTOM);
	jButtonCapture.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonCaptureActionPerformed(evt);
	    }
	});
	jToolBar1.add(jButtonCapture);

	jLabel16.setText("      ");
	jToolBar1.add(jLabel16);

	jButtonAudio.setFont(new java.awt.Font("微软雅黑", 0, 12));
	jButtonAudio.setForeground(new java.awt.Color(51, 51, 255));
	jButtonAudio.setText("Mute  ");
	jButtonAudio.setFocusable(false);
	jButtonAudio.setHorizontalTextPosition(SwingConstants.CENTER);
	jButtonAudio.setVerticalTextPosition(SwingConstants.BOTTOM);
	jButtonAudio.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonAudioActionPerformed(evt);
	    }
	});
	jToolBar1.add(jButtonAudio);

	GroupLayout jPanelPlayControlLayout = new GroupLayout(jPanelPlayControl);
	jPanelPlayControl.setLayout(jPanelPlayControlLayout);
	jPanelPlayControlLayout
		.setHorizontalGroup(jPanelPlayControlLayout
			.createParallelGroup(GroupLayout.Alignment.LEADING)
			.addGroup(
				jPanelPlayControlLayout
					.createSequentialGroup()
					.addGroup(
						jPanelPlayControlLayout
							.createParallelGroup(
								GroupLayout.Alignment.LEADING)
							.addGroup(
								jPanelPlayControlLayout
									.createSequentialGroup()
									.addComponent(
										jToolBar1,
										GroupLayout.PREFERRED_SIZE,
										371,
										GroupLayout.PREFERRED_SIZE)
									.addGap(27,
										27,
										27)
									.addComponent(
										jSliderAudio,
										GroupLayout.PREFERRED_SIZE,
										107,
										GroupLayout.PREFERRED_SIZE))
							.addComponent(
								panelPlay,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
					.addContainerGap(
						GroupLayout.DEFAULT_SIZE,
						Short.MAX_VALUE)));
	jPanelPlayControlLayout
		.setVerticalGroup(jPanelPlayControlLayout
			.createParallelGroup(GroupLayout.Alignment.LEADING)
			.addGroup(
				jPanelPlayControlLayout
					.createSequentialGroup()
					.addComponent(panelPlay,
						GroupLayout.PREFERRED_SIZE,
						GroupLayout.DEFAULT_SIZE,
						GroupLayout.PREFERRED_SIZE)
					.addGap(11, 11, 11)
					.addGroup(
						jPanelPlayControlLayout
							.createParallelGroup(
								GroupLayout.Alignment.LEADING)
							.addComponent(
								jSliderAudio,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
							.addComponent(
								jToolBar1,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
					.addContainerGap()));

	jButtonDownload.setText("下载");
	jButtonDownload.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonDownloadActionPerformed(evt);
	    }
	});

	jButtonSave.setText("保存");
	jButtonSave.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonSaveActionPerformed(evt);
	    }
	});

	jButtonExit.setText("退出");
	jButtonExit.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButtonExitActionPerformed(evt);
	    }
	});

	GroupLayout layout = new GroupLayout(getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addComponent(
									jPanelPlayControl,
									GroupLayout.PREFERRED_SIZE,
									538,
									GroupLayout.PREFERRED_SIZE)
								.addGap(10, 10,
									10)
								.addGroup(
									layout.createParallelGroup(
										GroupLayout.Alignment.LEADING)
										.addComponent(
											jButtonDownload,
											GroupLayout.DEFAULT_SIZE,
											81,
											Short.MAX_VALUE)
										.addComponent(
											jButtonSave,
											GroupLayout.DEFAULT_SIZE,
											81,
											Short.MAX_VALUE)
										.addComponent(
											jButtonExit,
											GroupLayout.DEFAULT_SIZE,
											81,
											Short.MAX_VALUE)))
						.addComponent(
							jPanelTime,
							GroupLayout.PREFERRED_SIZE,
							GroupLayout.DEFAULT_SIZE,
							GroupLayout.PREFERRED_SIZE))
				.addContainerGap()));
	layout.setVerticalGroup(layout
		.createParallelGroup(GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jPanelTime,
					GroupLayout.PREFERRED_SIZE,
					GroupLayout.DEFAULT_SIZE,
					GroupLayout.PREFERRED_SIZE)
				.addGroup(
					layout.createParallelGroup(
						GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addGap(249,
									249,
									249)
								.addComponent(
									jButtonDownload)
								.addGap(30, 30,
									30)
								.addComponent(
									jButtonSave)
								.addGap(80, 80,
									80)
								.addComponent(
									jButtonExit))
						.addGroup(
							layout.createSequentialGroup()
								.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									jPanelPlayControl,
									GroupLayout.PREFERRED_SIZE,
									430,
									GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(GroupLayout.DEFAULT_SIZE,
					Short.MAX_VALUE)));

	pack();
    }// </editor-fold>//GEN-END:initComponents

    /*************************************************
     * 函数: "退出" 按钮单击相应函数 函数描述: 销毁对话框
     *************************************************/
    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonExitActionPerformed
    {// GEN-HEADEREND:event_jButtonExitActionPerformed
	StopPlay();
	dispose();
    }// GEN-LAST:event_jButtonExitActionPerformed

    /*************************************************
     * 函数: "声音进度条" 鼠标离开响应函数 函数描述: 调节声音
     *************************************************/
    private void jSliderAudioMouseReleased(java.awt.event.MouseEvent evt)// GEN-FIRST:event_jSliderAudioMouseReleased
    {// GEN-HEADEREND:event_jSliderAudioMouseReleased
	if (m_lPlayHandle.intValue() >= 0) {
	    if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
		    HCNetSDK.NET_DVR_PLAYAUDIOVOLUME, jSliderAudio.getValue(),
		    null)) {
		System.out.println(" Set volume Succeed!");
	    } else {
		System.out.println(" Set volume Failed!");
	    }
	}
    }// GEN-LAST:event_jSliderAudioMouseReleased

    /*************************************************
     * 函数: "下载" 按钮单击相应函数 函数描述: 开始或停止下载文件
     *************************************************/
    private void jButtonDownloadActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonDownloadActionPerformed
    {// GEN-HEADEREND:event_jButtonDownloadActionPerformed
	if (m_lLoadHandle.intValue() == -1) {
	    HCNetSDK.NET_DVR_TIME struStartTime;
	    HCNetSDK.NET_DVR_TIME struStopTime;

	    struStartTime = new HCNetSDK.NET_DVR_TIME();
	    struStopTime = new HCNetSDK.NET_DVR_TIME();
	    struStartTime.dwYear = Integer.parseInt(jTextFieldsYear.getText());// 开始时间
	    struStartTime.dwMonth = Integer
		    .parseInt(jTextFieldsMonth.getText());
	    struStartTime.dwDay = Integer.parseInt(jTextFieldsDay.getText());
	    struStartTime.dwHour = Integer.parseInt(jTextFieldsHour.getText());
	    struStartTime.dwMinute = Integer.parseInt(jTextFieldsMinute
		    .getText());
	    struStartTime.dwSecond = Integer.parseInt(jTextFieldsSecond
		    .getText());
	    struStopTime.dwYear = Integer.parseInt(jTextFieldeYear.getText());// 结束时间
	    struStopTime.dwMonth = Integer.parseInt(jTextFieldeMonth.getText());
	    struStopTime.dwDay = Integer.parseInt(jTextFieldeDay.getText());
	    struStopTime.dwHour = Integer.parseInt(jTextFieldeHour.getText());
	    struStopTime.dwMinute = Integer.parseInt(jTextFieldeMinute
		    .getText());
	    struStopTime.dwSecond = Integer.parseInt(jTextFieldeSecond
		    .getText());

	    m_iChanShowNum = Integer.parseInt(jTextFieldChanNum.getText());

	    String sFileName = "c:/DownLoad/" + m_sDeviceIP + m_iChanShowNum
		    + struStartTime.toStringTitle()
		    + struStopTime.toStringTitle() + ".mp4";
	    System.out.println(sFileName);
	    m_lLoadHandle = hCNetSDK.NET_DVR_GetFileByTime(m_lUserID,
		    new NativeLong(m_iChanShowNum), struStartTime,
		    struStopTime, sFileName);
	    if (m_lLoadHandle.intValue() >= 0) {
		hCNetSDK.NET_DVR_PlayBackControl(m_lLoadHandle,
			HCNetSDK.NET_DVR_PLAYSTART, 0, null);
		jButtonDownload.setText("停止下载");
		Downloadtimer = new Timer();// 新建定时器
		Downloadtimer.schedule(new DownloadTask(), 0, 5000);// 0秒后开始响应函数
	    } else {
		JOptionPane.showMessageDialog(this, "按时间下载失败");
		System.out.println("laste error "
			+ hCNetSDK.NET_DVR_GetLastError());
		return;
	    }
	} else {
	    hCNetSDK.NET_DVR_StopGetFile(m_lLoadHandle);
	    jButtonDownload.setText("下载");
	    Downloadtimer.cancel();
	}
    }// GEN-LAST:event_jButtonDownloadActionPerformed

    /*************************************************
     * 函数: "保存" 按钮单击相应函数 函数描述: 开始或停止保存文件
     *************************************************/
    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonSaveActionPerformed
    {// GEN-HEADEREND:event_jButtonSaveActionPerformed
	if (m_lPlayHandle.intValue() == -1) {
	    JOptionPane.showMessageDialog(this, "请先播放文件");
	    return;
	}
	if (!m_bTimeSave) {
	    JFileChooser myJFileChooser = new JFileChooser();
	    myJFileChooser.showSaveDialog(this);
	    String sFileName = myJFileChooser.getSelectedFile() + ".mp4";
	    if (hCNetSDK.NET_DVR_PlayBackSaveData(m_lPlayHandle, sFileName)) {
		m_bTimeSave = true;
		jButtonSave.setText("停止保存");
	    }
	} else {
	    hCNetSDK.NET_DVR_StopPlayBackSave(m_lPlayHandle);
	    m_bTimeSave = false;
	    jButtonSave.setText("保存");
	}
    }// GEN-LAST:event_jButtonSaveActionPerformed

    /*************************************************
     * 函数: "Play" 按钮单击相应函数 函数描述: 开始或暂停按时间回放
     *************************************************/
    private void jButtonPlayActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonPlayActionPerformed
    {// GEN-HEADEREND:event_jButtonPlayActionPerformed
	HCNetSDK.NET_DVR_TIME struStartTime;
	HCNetSDK.NET_DVR_TIME struStopTime;

	if (m_lPlayHandle.intValue() == -1) {
	    m_iChanShowNum = Integer.parseInt(jTextFieldChanNum.getText());

	    struStartTime = new HCNetSDK.NET_DVR_TIME();
	    struStopTime = new HCNetSDK.NET_DVR_TIME();

	    struStartTime.dwYear = Integer.parseInt(jTextFieldsYear.getText());// 开始时间
	    struStartTime.dwMonth = Integer
		    .parseInt(jTextFieldsMonth.getText());
	    struStartTime.dwDay = Integer.parseInt(jTextFieldsDay.getText());
	    struStartTime.dwHour = Integer.parseInt(jTextFieldsHour.getText());
	    struStartTime.dwMinute = Integer.parseInt(jTextFieldsMinute
		    .getText());
	    struStartTime.dwSecond = Integer.parseInt(jTextFieldsSecond
		    .getText());
	    struStopTime.dwYear = Integer.parseInt(jTextFieldeYear.getText());// 结束时间
	    struStopTime.dwMonth = Integer.parseInt(jTextFieldeMonth.getText());
	    struStopTime.dwDay = Integer.parseInt(jTextFieldeDay.getText());
	    struStopTime.dwHour = Integer.parseInt(jTextFieldeHour.getText());
	    struStopTime.dwMinute = Integer.parseInt(jTextFieldeMinute
		    .getText());
	    struStopTime.dwSecond = Integer.parseInt(jTextFieldeSecond
		    .getText());

	    // 获取窗口句柄
	    HWND hwnd = new HWND(Native.getComponentPointer(panelPlay));

	    m_lPlayHandle = hCNetSDK.NET_DVR_PlayBackByTime(m_lUserID,
		    new NativeLong(m_iChanShowNum), struStartTime,
		    struStopTime, hwnd);

	    if (m_lPlayHandle.intValue() == -1) {
		JOptionPane.showMessageDialog(this, "按时间回放失败");
		return;
	    } else {
		// 还要调用该接口才能开始回放
		hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYSTART, 0, null);
		System.out.println("开始回放");
	    }

	    if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
		    HCNetSDK.NET_DVR_PLAYSTARTAUDIO, 0, null)) {
		m_bSound = true;
		hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYAUDIOVOLUME, (0xffff) / 2, null);
		System.out.println("开始声音");
		jSliderAudio.setValue(50);
		jButtonAudio.setText("Audio");
	    } else {
		m_bSound = false;
		jButtonAudio.setText("Mute");
	    }

	    // 开始计时器
	    Playbacktimer = new Timer();// 新建定时器
	    Playbacktimer.schedule(new PlaybackTask(), 0, 5000);// 0秒后开始响应函数
	} else {
	    if (m_bPause) {
		if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYRESTART, 0, null)) {
		    System.out.println("Start");
		    jButtonPlay.setText("Pause");
		    m_bPause = false;
		}
	    } else {
		if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYPAUSE, 0, null)) {
		    System.out.println("Pause");
		    m_bPause = true;
		    jButtonPlay.setText("Start");
		}
	    }
	}
    }// GEN-LAST:event_jButtonPlayActionPerformed

    /*************************************************
     * 函数: "Stop" 按钮单击相应函数 函数描述: 停止回放
     *************************************************/
    private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonStopActionPerformed
    {// GEN-HEADEREND:event_jButtonStopActionPerformed
	StopPlay();
    }// GEN-LAST:event_jButtonStopActionPerformed

    /*************************************************
     * 函数: "Slow" 按钮单击相应函数 函数描述: 慢放
     *************************************************/
    private void jButtonSlowActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonSlowActionPerformed
    {// GEN-HEADEREND:event_jButtonSlowActionPerformed
	hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
		HCNetSDK.NET_DVR_PLAYSLOW, 0, null);
    }// GEN-LAST:event_jButtonSlowActionPerformed

    /*************************************************
     * 函数: "Fast" 按钮单击相应函数 函数描述: 快放
     *************************************************/
    private void jButtonFastActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonFastActionPerformed
    {// GEN-HEADEREND:event_jButtonFastActionPerformed
	hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
		HCNetSDK.NET_DVR_PLAYFAST, 0, null);
    }// GEN-LAST:event_jButtonFastActionPerformed

    /*************************************************
     * 函数: "Capture" 按钮单击相应函数 函数描述: 截图
     *************************************************/
    private void jButtonCaptureActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonCaptureActionPerformed
    {// GEN-HEADEREND:event_jButtonCaptureActionPerformed
	if (m_lPlayHandle.intValue() == -1) {
	    return;
	}
	String sPicName = "C:/Picture/" + m_iChanShowNum
		+ System.currentTimeMillis() + ".bmp";
	if (hCNetSDK.NET_DVR_PlayBackCaptureFile(m_lPlayHandle, sPicName)) {
	    System.out.println("抓图:" + sPicName);
	    return;
	} else {
	    JOptionPane.showMessageDialog(this, "抓图失败");
	}
    }// GEN-LAST:event_jButtonCaptureActionPerformed

    /*************************************************
     * 函数: "Audio" 按钮单击相应函数 函数描述: 开启或关闭声音
     *************************************************/
    private void jButtonAudioActionPerformed(java.awt.event.ActionEvent evt)// GEN-FIRST:event_jButtonAudioActionPerformed
    {// GEN-HEADEREND:event_jButtonAudioActionPerformed
	if (m_lPlayHandle.intValue() >= 0) {
	    if (m_bSound) {
		if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYSTOPAUDIO, 0, null)) {
		    m_bSound = false;
		    jButtonAudio.setText("Mute");
		} else {
		    System.out.println("Stop audio failed!");
		}
	    } else {
		if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYSTARTAUDIO, 0, null)) {
		    jButtonAudio.setText("Audio");
		    m_bSound = true;
		} else {
		    System.out.println("Start audio failed!");
		}
	    }
	}
    }// GEN-LAST:event_jButtonAudioActionPerformed

    /*************************************************
     * 函数: initialDialog 函数描述: 初始化Dialog
     *************************************************/
    private void initialDialog() {
	Date today = new Date();// 现在时间,用于设置时间框里的时间(当天的0:0:0-23:59:59)
	Calendar now = Calendar.getInstance();// 日历对象
	now.setTime(today); // 设置时间

	// 开始时间
	jTextFieldsYear.setText(now.get(Calendar.YEAR) + "");
	jTextFieldsMonth.setText((now.get(Calendar.MONTH) + 1) + "");
	jTextFieldsDay.setText(1 + "");
	jTextFieldsHour.setText("0");
	jTextFieldsMinute.setText("0");
	jTextFieldsSecond.setText("0");

	// 结束时间
	jTextFieldeYear.setText(now.get(Calendar.YEAR) + "");
	jTextFieldeMonth.setText((now.get(Calendar.MONTH) + 1) + "");
	jTextFieldeDay.setText(now.get(Calendar.DATE) + "");
	jTextFieldeHour.setText("23");
	jTextFieldeMinute.setText("59");
	jTextFieldeSecond.setText("59");

	this.addWindowListener(new WindowAdapter() {
	    @Override
	    public void windowClosing(WindowEvent e) {
		StopPlay();
	    }
	});
    }

    /*************************************************
     * 类: DownloadTask 类描述: 下载定时器响应函数
     *************************************************/
    class DownloadTask extends java.util.TimerTask {
	// 定时器函数
	@Override
	public void run() {
	    IntByReference nPos = new IntByReference(0);
	    hCNetSDK.NET_DVR_PlayBackControl(m_lLoadHandle,
		    HCNetSDK.NET_DVR_PLAYGETPOS, 0, nPos);
	    if (nPos.getValue() > 100) {
		hCNetSDK.NET_DVR_StopGetFile(m_lLoadHandle);
		m_lLoadHandle.setValue(-1);
		jButtonDownload.setText("下载");
		Downloadtimer.cancel();
		JOptionPane.showMessageDialog(null, "由于网络原因或DVR忙,下载异常终止!");
	    }
	    if (nPos.getValue() == 100) {
		hCNetSDK.NET_DVR_StopGetFile(m_lLoadHandle);
		m_lLoadHandle.setValue(-1);
		jButtonDownload.setText("下载");
		Downloadtimer.cancel();
		JOptionPane.showMessageDialog(null, "按时间下载结束!");
	    }
	}
    }

    /*************************************************
     * 类: PlaybackTask 类描述: 下载定时器响应函数
     *************************************************/
    class PlaybackTask extends java.util.TimerTask {
	// 定时器函数
	@Override
	public void run() {
	    IntByReference nPos = new IntByReference(0);
	    if (m_lPlayHandle.intValue() >= 0) {
		if (hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
			HCNetSDK.NET_DVR_PLAYGETPOS, 0, nPos)) {
		    System.out.println("回放进度" + nPos.getValue());
		} else {
		    System.out.println("获取回放进度失败");
		}

		if (nPos.getValue() > 100) {
		    StopPlay();
		    JOptionPane.showMessageDialog(null, "由于网络原因或DVR忙,回放异常终止!");
		}
		if (nPos.getValue() == 100) {
		    StopPlay();
		    JOptionPane.showMessageDialog(null, "按时间回放结束");
		}
	    }
	}
    }

    /*************************************************
     * 函数: StopPlay 函数描述: 停止回放的相关操作
     *************************************************/
    private void StopPlay() {
	if (m_lPlayHandle.intValue() >= 0) {
	    if (m_bTimeSave) {
		hCNetSDK.NET_DVR_StopPlayBackSave(m_lPlayHandle);
		jButtonSave.setText("保存");
		m_bTimeSave = false;
	    }

	    hCNetSDK.NET_DVR_PlayBackControl(m_lPlayHandle,
		    HCNetSDK.NET_DVR_PLAYSTOPAUDIO, 0, null);
	    hCNetSDK.NET_DVR_StopPlayBack(m_lPlayHandle);
	    m_lPlayHandle.setValue(-1);

	    Playbacktimer.cancel();
	    panelPlay.repaint();
	}
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton jButtonAudio;
    private JButton jButtonCapture;
    private JButton jButtonDownload;
    private JButton jButtonExit;
    private JButton jButtonFast;
    private JButton jButtonPlay;
    private JButton jButtonSave;
    private JButton jButtonSlow;
    private JButton jButtonStop;
    private JLabel jLabel1;
    private JLabel jLabel10;
    private JLabel jLabel11;
    private JLabel jLabel12;
    private JLabel jLabel13;
    private JLabel jLabel14;
    private JLabel jLabel15;
    private JLabel jLabel16;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JLabel jLabel9;
    private JLabel jLabelEndTime;
    private JLabel jLabelStartTime;
    private JPanel jPanelPlayControl;
    private JPanel jPanelTime;
    private JSlider jSliderAudio;
    private JTextField jTextFieldChanNum;
    private JTextField jTextFieldeDay;
    private JTextField jTextFieldeHour;
    private JTextField jTextFieldeMinute;
    private JTextField jTextFieldeMonth;
    private JTextField jTextFieldeSecond;
    private JTextField jTextFieldeYear;
    private JTextField jTextFieldsDay;
    private JTextField jTextFieldsHour;
    private JTextField jTextFieldsMinute;
    private JTextField jTextFieldsMonth;
    private JTextField jTextFieldsSecond;
    private JTextField jTextFieldsYear;
    private JToolBar jToolBar1;
    private Panel panelPlay;
    // End of variables declaration//GEN-END:variables

}
